package com.suning.sawp.service.impl.mainpush;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.mainpush.OperationLogService;
import com.suning.sawp.po.mainpush.OptLog;
import com.suning.sawp.service.util.SingleThreadFactory;

/**
 * 〈将用户的操作记录到数据库中〉<br>
 * 〈对用户的行为进行记录，防止恶意篡改数据〉
 *
 * @author 12061748
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class OperationLogServiceImpl implements OperationLogService {
    @Resource
    private DalClient dalClient;

    @Override
    public void log(final OptLog log) {
        /**
         * 异步保存用户操作行为
         */
        SingleThreadFactory.getExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                asyncSaveOperationRecord(log);
            }
        });
    }

    /**
     * 
     * 功能描述: 异步保存用户操作记录(超级管理员的操作不记录)<br>
     * 〈功能详细描述〉
     *
     * @param loginInfo
     * @param eventType
     * @param eventDetail
     * @param content
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void asyncSaveOperationRecord(OptLog log) {
        dalClient.persist(log);
    }

    @Override
    public List<OptLog> queryOperationLog(String taskNo, String startTime, String endTime, Integer start, Integer limit) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        paramMap.put("start", start);
        paramMap.put("limit", limit);
        return dalClient.queryForList("optLog.queryOptLogList", paramMap, OptLog.class);
    }

    @Override
    public int queryOptLogCount(String taskNo, String startTime, String endTime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskNo", taskNo);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        Map<String, Object> countMap = dalClient.queryForMap("optLog.queryOptLogListCount", paramMap);
        return ((Long) countMap.get("num")).intValue();
    }

}
